//
//  ArticleWebviewViewController.m
//  Adorable_Things
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "ArticleDetailViewController.h"
#import "ArticleDetailMengwenViewController.h"
#import "ATCommentViewController.h"
#import "TTTaobaoHeaderView.h"
#import "TTDiscoverMainViewController.h"
#import "ATLoginViewController.h"
#define kArticleDetailUrl @"http://api.menghuoapp.com/v1/post/detail"
typedef void(^Block)();

@interface ArticleDetailViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) ArticleDetailMengwenViewController *mengwenVC;
@property (nonatomic, strong) ATCommentViewController *commentVC;
@property (nonatomic, strong) TTTaobaoHeaderView *headerView;
@property (nonatomic, copy) Block IDBlock;
@property (nonatomic, assign) NSInteger isLike;

@end

@implementation ArticleDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = YES;
    
    _isLike = [_model.favorite integerValue];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.headerView = [[TTTaobaoHeaderView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 45)];
    [self.view addSubview:self.headerView];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 65, kWidth, kHeight - 65)];
    self.scrollView.contentSize = CGSizeMake(kWidth * 2, kHeight - 65);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    [self.view addSubview:self.scrollView];
    
    [self.headerView.backButton addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    if ([_model.favorite integerValue] == 0) {
      [self.headerView.favorButton setImage:[UIImage imageNamed:@"iconfont-tubiao"] forState:(UIControlStateNormal)];
    } else{
      [self.headerView.favorButton setImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
    }
    
    [self.headerView.favorButton addTarget:self action:@selector(favorButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    [self.headerView.seg addTarget:self action:@selector(transformView:) forControlEvents:UIControlEventValueChanged];

    _mengwenVC = [[ArticleDetailMengwenViewController alloc] init];
    _mengwenVC.ID = self.ID;
    _mengwenVC.view.frame = CGRectMake(0, 0, kWidth, kHeight - 65);
    [self.scrollView addSubview:_mengwenVC.view];
    [self addChildViewController:_mengwenVC];
    
    self.commentVC = [[ATCommentViewController alloc] init];
    _commentVC.post_ID = self.ID;
    _commentVC.view.frame = CGRectMake(kWidth, 0, kWidth, kHeight - 65 - 50);
    [self.scrollView addSubview:_commentVC.view];
    [self addChildViewController:_commentVC];
    
    __weak ATCommentViewController *mySelf = _commentVC;
    self.mengwenVC.IDBlock = ^ void(NSNumber *topic_type){
        mySelf.topic_ID = topic_type;
        [mySelf setUpData];
    };    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat scrollx = _scrollView.contentOffset.x / kWidth;
    CGFloat indexMengwen = self.headerView.mengwenLabel.tag - 201;
    CGFloat indexComment = self.headerView.commentLabel.tag - 201;
    if (scrollx == indexMengwen) {
        self.headerView.mengwenLabel.backgroundColor = [UIColor redColor];
        self.headerView.commentLabel.backgroundColor = [UIColor whiteColor];
    } else if (scrollx == indexComment){
        self.headerView.mengwenLabel.backgroundColor = [UIColor whiteColor];
        self.headerView.commentLabel.backgroundColor = [UIColor redColor];
    }
 
}

- (void)buttonClick:(UIButton *)button
{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)transformView:(UISegmentedControl *)segmentedControl
{
    if (segmentedControl.selectedSegmentIndex == 0) {
        [UIView animateWithDuration:.5 animations:^{
            self.headerView.mengwenLabel.backgroundColor = [UIColor redColor];
            self.headerView.commentLabel.backgroundColor = [UIColor whiteColor];
            [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

        }];
    } else {
        [UIView animateWithDuration:.5 animations:^{
            self.headerView.mengwenLabel.backgroundColor = [UIColor whiteColor];
            self.headerView.commentLabel.backgroundColor = [UIColor redColor];
            [_scrollView setContentOffset:CGPointMake(kWidth, 0) animated:NO];

        }];
    }
}

- (void)favorButtonClick
{
    ATUserManager *manager = [ATUserManager shareManager];
    _isLike = !_isLike;
    if (manager.isLogin == 0) {
        ATLoginViewController *loginVC = [[ATLoginViewController alloc] init];
        UINavigationController *loginNavC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        loginNavC.navigationBarHidden = YES;
        [self presentViewController:loginNavC animated:YES completion:nil];
    }
    else{
        
        NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
        parDic[@"auth_token"] = manager.auth_token;
        parDic[@"post_id"] = self.model.post_id;
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        if (_isLike) {
            [manager POST:@"http://api.menghuoapp.com/v1/post/mark" parameters:parDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSLog(@"收藏成功");
                [self.headerView.favorButton setImage:[UIImage imageNamed:@"iconfont-xihuan-2"] forState:(UIControlStateNormal)];
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"收藏失败");
            }];
    } else {
        [manager POST:@"http://api.menghuoapp.com/v1/post/unmark" parameters:parDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSLog(@"取消成功");
            [self.headerView.favorButton setImage:[UIImage imageNamed:@"iconfont-tubiao"] forState:(UIControlStateNormal)];
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"取消失败");
            }];
        }
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_mengwenVC setUpData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
